﻿using CatUtils.Utils.Currency.TextUtils;
using System;
using System.Text;

namespace CatUtils.Utils.SpecialUtils.SystemLock
{
    internal class SystemLockLocal
    {
        /// <summary>
        /// 默认干扰长度
        /// </summary>
        private static int DeflautNoisteLength = 15;

        public static string GetVerification()
        {
            return GetVerification(DeflautNoisteLength);
        }

        /// <summary>
        /// 生成加密密钥
        /// </summary>

        /// <returns></returns>
        public static string GetVerification(int NoisteLength)
        {
            int Key = (DateTime.Now.Day + DateTime.Now.Month + 20) * 13573;

            byte[] vs = TextHelper.StringToByte(Key.ToString());
            Array.Reverse(vs);
            return TextHelper.GetRandomString(NoisteLength) + TextHelper.ByteToString(vs, Encoding.UTF8);
        }

        public static bool VerificationKey(string Key)
        {
            return VerificationKey(Key, DeflautNoisteLength);
        }

        /// <summary>
        /// 验证密钥是否正确
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static bool VerificationKey(string Key, int NoisteLength)
        {
            if (Key.Length < 10)
                return false;

            Key = Key.Substring(NoisteLength);

            string GetKey = GetVerification(NoisteLength).Substring(NoisteLength);
            if (Key == GetKey)
                return true;
            return false;
        }
    }
}